package com.cqc.paoclient.ui.code;

import com.cqc.paoclient.http.RetrofitHelper;
import com.cqc.paoclient.model.CodeModel;
import com.cqc.paoclient.util.NetErrorUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by cui on 2017/11/26.
 */

public class Tab2Presenter extends Tab2Contract.Presenter {

    int p=0;

    /**
     * 获取第一页数据
     */
    @Override
    void getCodeList() {
        disposable.add(RetrofitHelper.createCodeApi().codeList(0)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CodeModel>() {
                    @Override
                    public void accept(CodeModel codeModel) throws Exception {
                        mView.getCodeListSuccess(codeModel);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.getCodeListFail(NetErrorUtil.handleThrowable(throwable));
                    }
                }));
    }

    @Override
    void loadMoreList() {
        disposable.add(RetrofitHelper.createCodeApi().codeList(p++)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CodeModel>() {
                    @Override
                    public void accept(CodeModel codeModel) throws Exception {
                        mView.loadMoreSuccess(codeModel);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.getCodeListFail(NetErrorUtil.handleThrowable(throwable));
                    }
                }));
    }
}
